package com.jc.oms.controller.port.in;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.Base64Util;
import com.jc.common.util.ExceptionUtil;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.framework.enums.Conclusion;
import com.jc.framework.enums.LogType;
import com.jc.framework.web.ResponseBaseModel;
import com.jc.oms.controller.BaseController;
import com.jc.service.api.Api;
import com.jc.service.api.ParamModel;
import com.jc.service.api.yx163.callback.YanXuanCallBackService;
import com.jc.service.jg.JgService;
import com.jc.service.model.yx.YanXuanResult;
import com.jc.service.systemloglist.SystemLogListService;

@Controller
@RequestMapping("api")
public class ApiController extends BaseController {
	
	@Autowired
	private Api api;
	@Autowired 
	SystemLogListService systemLogListService;
	@Autowired
	YanXuanCallBackService yanXuanCallBackService;
	
	@Autowired
	JgService jgService;
	
	
	
	/***
	 * 
	* @Title: proess  外部系统主动调用OMS的统一API入口,里面包含了统一签名校验.
	* @Description: TODO
	* @param @param paramObject
	* @param @return
	* @return ResponseEntity<?>
	* @throws
	 */
	@RequestMapping(method = RequestMethod.POST , consumes = "application/json;charset=utf8")
	public @ResponseBody ResponseEntity<?> proess(@RequestBody ParamModel paramObject){
		
		try {
			ResponseEntity<?> ret = api.deal(paramObject);
			systemLogListService.addSystemLog(JSONObject.toJSONString(paramObject), Conclusion.OK, null, LogType.RECEIVE_H5_ORDER, JSONObject.toJSONString(ret));
			return ret;
		}catch (Exception e) {
			ResponseEntity<?> ret = new ResponseEntity<>(new ResponseBaseModel(29001,e.getMessage()), HttpStatus.OK);
			systemLogListService.addSystemLog(JSONObject.toJSONString(paramObject), Conclusion.FAIL, ExceptionUtil.subExceptionMessage(e, 0, 150), LogType.RECEIVE_H5_ORDER, JSONObject.toJSONString(ret));
			return ret;
		}
	}
	
	
	
	
	/***
	 * 
	* @Title: callback 网易回调API入口，里面暂时不包含签名验证
	* @Description: TODO
	* @param @param paramObject
	* @param @return
	* @return ResponseEntity<?>
	* @throws
    */
	@RequestMapping(value="163" , method = RequestMethod.POST )
	public @ResponseBody YanXuanResult callback163(HttpServletRequest paramObject){
		YanXuanResult ret = new YanXuanResult();
		Map<String,Object> param = new HashMap<String, Object>();
		try {
			param = readParamToMap(paramObject);
			
			if(param == null || param.size() < 1) {
				ret.setCode(2221);
				ret.setMessage("参数为空");
				ret.setResult("");
				return ret;
			}
			ret = yanXuanCallBackService.callBack(param);
			if(ret != null && StringUtils.equals("200", ret.getCode())) {
				systemLogListService.addSystemLog(JSONObject.toJSONString(param), Conclusion.OK, "", LogType.YANXUAN_CALLBACK_API, JSONObject.toJSONString(ret));
			}else {
				systemLogListService.addSystemLog(JSONObject.toJSONString(param), Conclusion.FAIL, "", LogType.YANXUAN_CALLBACK_API, JSONObject.toJSONString(ret));
			}
			return ret;
		}catch(Exception e) {
			getLoger().error("网易严选回调错误 :  {},{}" , e.getMessage(),e);
			systemLogListService.addSystemLog(JSONObject.toJSONString(param), Conclusion.FAIL, ExceptionUtil.subExceptionMessage(e, 0, 200), LogType.YANXUAN_CALLBACK_API, JSONObject.toJSONString(ret));
			ret.setCode(9999);
			ret.setMessage(ExceptionUtil.subExceptionMessage(e, 0, 200));
			return ret;
		}
	}
	
	
	@RequestMapping(value="jioao/express" , method = RequestMethod.POST , consumes = "application/json;charset=utf8")
	public @ResponseBody Map<String,Object> callexpress(HttpServletRequest paramObject,@RequestBody JSONObject jsonObject){
		Map<String,Object> ret = new HashMap<String, Object>();
		ret.put("code", 10000);
		ret.put("message", "ok");
		String _data = ""; 
		try {
			//不验签
			_data = jsonObject.getString("data");
			if(StringUtils.isBlank(_data)) {
				ret.put("code", 9999);
				ret.put("message", "缺少data参数");
				return ret;
			}
			_data = Base64Util.decode(_data);
			
			JSONObject expressObject = null;
			try {
				expressObject = JSON.parseObject(_data);
			}catch (Exception e) {
				//throw new BaseServiceException("格式化data参数错误,无法转换JSON格式");
				ret.put("code", 9999);
				ret.put("message", "格式化data参数错误,无法转换JSON格式");
				return ret;
			}
			if(expressObject != null) {
				String orderSn = expressObject.getString("orderSn");
				String expressName = expressObject.getString("expressName");
				String expressNo = expressObject.getString("expressNo");
				return jgService.addJgOrderExpress(orderSn, expressName, expressNo);
			}
			systemLogListService.addSystemLog(_data, Conclusion.OK, "", LogType.JG_ORDER_CALLBACK, JSONObject.toJSONString(ret));
			return ret;
		}catch(Exception e) {
			getLoger().error("吉贡订单出库回调错误 :  {},{}" , e.getMessage(),e);
			systemLogListService.addSystemLog(_data, Conclusion.FAIL, ExceptionUtil.subExceptionMessage(e, 0, 200), LogType.JG_ORDER_CALLBACK, JSONObject.toJSONString(ret));
			ret.put("code","9999");
			ret.put("message", ExceptionUtil.subExceptionMessage(e, 0, 200));
			return ret;
		}
	}
}
